

import plugin from '../../../../lib/plugins/plugin.js'
import common from "../../../../lib/common/common.js"
import config from "../../model/Config.js"
import { Read_Forum,Write_Forum } from './Forum.js'

/**
 * 定时任务
 */

export class ForumTask extends plugin {
    constructor() {
        super({
            name: 'ForumTask',
            dsc: '定时任务',
            event: 'message',
            priority: 300,
            rule: [
            ]
        });
        this.set = config.getdefSet('task', 'task')
        this.task = {
            cron: this.set.ForumTask,
            name: 'ForumTask',
            fnc: () => this.Forumtask()
        }
    }

    async Forumtask() {
        let Forum;
        try{
            Forum=await Read_Forum();
        }
        catch{
            await Write_Forum([]);
            Forum=await Read_Forum();
        }
        for (var i = 0; i < Forum.length; i++) {
                Forum = Forum.filter(item => item.qq != Forum[i].qq);
                Write_Forum(Forum);
        }
        return;
    }


    /**
     * 推送消息，群消息推送群，或者推送私人
     * @param id
     * @param is_group
     * @returns {Promise<void>}
     */
    async pushInfo(id, is_group, msg) {
        if (is_group) {
            await Bot.pickGroup(id)
                .sendMsg(msg)
                .catch((err) => {
                    Bot.logger.mark(err);
                });
        }
        else {
            await common.relpyPrivate(id, msg);
        }
    }
}
